package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Bean.User;
import Dao.UserDao;

public class ShowAllUserServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ArrayList userList = new ArrayList();
		UserDao userDao = new UserDao();
		ServletContext application=this.getServletContext();
		try {
			userList = userDao.queryAllUser();
			request.setAttribute("userList", userList);
			application.getRequestDispatcher("/showAllUser.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String username = request.getParameter("username");
		User user =new User();
		UserDao userDao = new UserDao();
		ServletContext application=this.getServletContext();
		try {
			user = userDao.queryUserByUsername(username);
			ArrayList userList = new ArrayList();
			userList.add(user);
			request.setAttribute("userList", userList);
			application.getRequestDispatcher("/showAllUser.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
